package com.dataex.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dataex.Constants;
import com.dataex.util.DBConnectionRegister;

public class DBService {

	public List<String> getTables(String databaseName) throws Exception {
		List<String> list = new ArrayList<String>();
		Connection connection = DBConnectionRegister.getConnection();
		try {
			ResultSet rs = connection.getMetaData().getTables("", "", "", null);
			while (rs.next()) {
				list.add(rs.getString("TABLE_NAME"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return list;
	}

	public List<String> getAllDatabases() throws Exception {
		List<String> list = new ArrayList<String>();
		Connection connection = DBConnectionRegister.getConnectionByUrl(Constants.URL_DB);
		PreparedStatement prepare = null;
		ResultSet rs = null;
		try {
			String sql = "show databases";
			prepare = connection.prepareStatement(sql);
			rs = prepare.executeQuery();
			while (rs.next()) {
				list.add(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnectionRegister.close(connection, prepare, rs);
		}
		return list;
	}

}
